package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.widget.CustomGridView;

/**
 * Created by 廉冲-android开发 on 2017/6/22.
 */
public class CircleImageAdapter extends BaseAdapter {
    private static final String DEFAULT_IMAGE = "default_image";
    private static final int MAX_IMAGE = 9;
    private Context context;
    private LayoutInflater inflater;
    private List<String> list = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onClickPreviewImage(List<String> imageList, int position);

        void onClickAddImage(View view, int position);
    }

    public CircleImageAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        list.add(DEFAULT_IMAGE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_published_grida, parent, false);
            holder = new ViewHolder();
            holder.iv = (ImageView) convertView.findViewById(R.id.item_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (((CustomGridView) parent).isOnMeasure) {
            return convertView;
        }

        if (list.size() == MAX_IMAGE) {
            if (isContainsDefaultImage()) {
                if (position == list.size() - 1) {
                    holder.iv.setImageResource(R.drawable.icon_tianjiazhaopian_n);
                } else {
                    Glide.with(context).load(new File(list.get(position))).into(holder.iv);
                }
            } else {
                Glide.with(context).load(new File(list.get(position))).into(holder.iv);
            }
        } else {
            if (position == list.size() - 1) {
                holder.iv.setImageResource(R.drawable.icon_tianjiazhaopian_n);
            } else {
                Glide.with(context).load(new File(list.get(position))).into(holder.iv);
            }
        }

        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.size() == MAX_IMAGE) {
                    if (isContainsDefaultImage()) {
                        if (position == list.size() - 1) {
                            mOnItemClickListener.onClickAddImage(view, position);
                        } else {
                            mOnItemClickListener.onClickPreviewImage(getSelectedList(), position);
                        }
                    } else {
                        mOnItemClickListener.onClickPreviewImage(list, position);
                    }
                } else {
                    if (position == list.size() - 1) {
                        mOnItemClickListener.onClickAddImage(view, position);
                    } else {
                        mOnItemClickListener.onClickPreviewImage(getSelectedList(), position);
                    }
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView iv;
    }

    public List<String> getList() {
        return list;
    }

    public ArrayList<String> getSelectedList() {
        ArrayList<String> selectedList = new ArrayList<>();
        for (String s : list) {
            if (!s.equals(DEFAULT_IMAGE)) {
                selectedList.add(s);
            }
        }
        if (selectedList != null && selectedList.size() > 0) {
            return selectedList;
        } else {
            return new ArrayList<>(0);
        }
    }

    public ArrayList<File> getSelectedImageList() {
        ArrayList<File> fileList = new ArrayList<>();
        for (String s : list) {
            if (!s.equals(DEFAULT_IMAGE)) {
                File file = new File(s);
                fileList.add(file);
            }
        }
        if (fileList != null && fileList.size() > 0) {
            return fileList;
        } else {
            return new ArrayList<>(0);
        }
    }

    public void addImageList(List<String> imageList) {
        if (imageList != null && imageList.size() > 0) {
            list.clear();
            if (imageList.size() == MAX_IMAGE) {
                for (String s : imageList) {
                    list.add(s);
                }
            } else {
                for (String s : imageList) {
                    list.add(s);
                }
                list.add(DEFAULT_IMAGE);
            }
            notifyDataSetChanged();
        }else{
            list.clear();
            list.add(DEFAULT_IMAGE);
        }
        notifyDataSetChanged();
    }

    public void addImage(String image) {
        if (!TextUtils.isEmpty(image)) {
            list.add(list.size() - 1, image);
            if (list.size() == MAX_IMAGE + 1) {
                list.remove(list.size() - 1);
            }
            notifyDataSetChanged();
        }
    }

    public boolean isContainsDefaultImage() {
        for (String s : list) {
            if (s.equals(DEFAULT_IMAGE)) {
                return true;
            }
        }
        return false;
    }


}
